package com.health.mapp.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.health.commons.PropertiesService;
import com.health.db.dto.StuffDetailDto;
import com.health.db.model.GoodsPriceListTemp;
import com.health.db.model.RegisterOrder;
import com.health.db.model.UserCollectFavorite;
import com.health.db.model.UserLIST;
import com.health.db.service.order.IRegisterOrderService;
import com.health.db.service.user.IGoodsPriceListService;
import com.health.db.service.user.IStuffListService;
import com.health.db.service.user.IUserCollectFavoriteService;
import com.health.db.service.user.IUserLISTService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.QueryDocDetailReq;
import com.health.mapp.bean.QueryDocDetailResp;
import com.health.mapp.bean.dto.DoctorDetailDto;
import com.health.mapp.bean.dto.RateDto;
import com.health.mapp.utils.Constant;
import com.health.mapp.utils.JudeMobileAttribution;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * Created by helili on 2017/2/20.
 */
public class QueryDocDetailImpl implements ActionInterface {

    private Logger logger = Logger.getLogger(QueryDocDetailImpl.class);
    private IStuffListService stuffListService  ;
    private IGoodsPriceListService goodsPriceListService;
    private IUserCollectFavoriteService userCollectFavoriteService  ;
    private IRegisterOrderService registerOrderService  ;
    
    private IUserLISTService userLISTService;

    @Override
    public Response execute(Header header, Object reqBean) throws Exception {
        QueryDocDetailReq req = (QueryDocDetailReq) reqBean;
        QueryDocDetailResp resp = new QueryDocDetailResp();
        
        Long docId = req.getDocId();
        Long userId = req.getUserId();
        logger.info("QueryDocDetail docId:" + docId + ",userId:" + userId);
        if (docId ==null) {
            resp.setErrorMsg("医生ID不能为空");
            resp.setErrorCode(-1);
            return resp;
        }
        StuffDetailDto stuffDetailDto = stuffListService.selectByStuffId(docId);
        if (stuffDetailDto == null) {
            resp.setErrorMsg("该医生不存在");
            resp.setErrorCode(-1);
            return resp;
        }
       
        UserCollectFavorite collectFavorite=null;
        if (userId != null) {
            collectFavorite = userCollectFavoriteService.selectByCollectorIdAndCollectionId(userId, docId);
        }

        DoctorDetailDto detailDto = new DoctorDetailDto();
        detailDto.setDocId(String.valueOf(stuffDetailDto.getStuffid()));
        detailDto.setDocName(stuffDetailDto.getStuffName());
        detailDto.setDocTitle(stuffDetailDto.getStuffTitle());
        detailDto.setDocLable(stuffDetailDto.getStuffLabel());
        detailDto.setDocImageUrl(stuffDetailDto.getIconUrl());
        detailDto.setDocImageUrl((stuffDetailDto.getIconUrl())==null?PropertiesService.getString("iconUrl.docDefaultUrl").trim():stuffDetailDto.getIconUrl());
        
        
        detailDto.setHospitalId(stuffDetailDto.getEntityid()==null?"":String.valueOf(stuffDetailDto.getEntityid()));
        detailDto.setHospitalName(stuffDetailDto.getHospitalName());
        detailDto.setDepartmentId(stuffDetailDto.getDepartmentid()==null?"":String.valueOf(stuffDetailDto.getDepartmentid()));
        detailDto.setDepartmentName(stuffDetailDto.getDepartmentName());
        //子科室
        detailDto.setSubDepartmentId(stuffDetailDto.getDepartmentid()==null?"":String.valueOf(stuffDetailDto.getDepartmentid()));
        
        detailDto.setSubDepartmentName(stuffDetailDto.getDepartmentName());
        detailDto.setConsultRecommend(stuffDetailDto.getConsultRecommend());
        detailDto.setConsultImpression(stuffDetailDto.getConsultImpression());
        //订单类型
        //String orderTypeStr = stuffDetailDto.getRegisterWhere();
       
        detailDto.setOrderType(stuffDetailDto.getRegistertypeid());

        detailDto.setDocBriefInfo(stuffDetailDto.getBriefInfo());
        detailDto.setConsultInstructions(stuffDetailDto.getConsultInstructions());
        
        detailDto.setOrderCount(stuffDetailDto.getOrderCount()==null?0:stuffDetailDto.getOrderCount());
        detailDto.setReceiveSpeed(stuffDetailDto.getReceiveSpeed()==null?"":stuffDetailDto.getReceiveSpeed());
        detailDto.setEvaluateScore(stuffDetailDto.getEvaluateScore()==null?"5.0":String.valueOf(stuffDetailDto.getEvaluateScore().setScale(1, BigDecimal.ROUND_DOWN)));
        

        if(collectFavorite == null){
            detailDto.setIsCollect(0);//未收藏
        }else {
            detailDto.setIsCollect(1);//已收藏
        }



        List<RegisterOrder> orderList = registerOrderService.selectOrderEvaluateByDocId(docId);
        List<RateDto> rateDtoList = new ArrayList<RateDto>();
        for (RegisterOrder order : orderList) {
            RateDto rateDto = new RateDto();
            rateDto.setOrderId(order.getRegisterid().toString());
            rateDto.setUserName(order.getUserName());
            rateDto.setEvaluateScore(order.getEvaluateScore().toString());
            rateDto.setEvaluateDescribe(order.getEvaluateDescribe());
            rateDto.setEvaluateDate(order.getRegisterDate());
            rateDtoList.add(rateDto);
        }
        detailDto.setRateList(rateDtoList);

        List<GoodsPriceListTemp> docPriceList=goodsPriceListService.selectByDocIdForUserUsed(Long.valueOf(docId));
        
        if (userId != null) {
            UserLIST userLIST= userLISTService.selectByUserId(userId);
            if (Constant.CONSTANTS_FREE_PROVICE_NAME.equals(JudeMobileAttribution.getMobileAttribution(userLIST.getUserNo()))) {
            	
            	 for (int i = 0; i < docPriceList.size(); i++) {
            		 GoodsPriceListTemp temp = docPriceList.get(i);
            		 if (temp.getGoodsId().equals("8")) {
            			 docPriceList.get(i).setPrice("0.00");
            			 break;
					}
				}
			}
        }
        detailDto.setDocPriceList(docPriceList);
        resp.setDocDetail(detailDto);
        resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
        resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
        return resp;
    }
   
	public IStuffListService getStuffListService() {
		return stuffListService;
	}

	public void setStuffListService(IStuffListService stuffListService) {
		this.stuffListService = stuffListService;
	}

	public IUserCollectFavoriteService getUserCollectFavoriteService() {
		return userCollectFavoriteService;
	}

	public void setUserCollectFavoriteService(
			IUserCollectFavoriteService userCollectFavoriteService) {
		this.userCollectFavoriteService = userCollectFavoriteService;
	}

	public IGoodsPriceListService getGoodsPriceListService() {
		return goodsPriceListService;
	}

	public void setGoodsPriceListService(
			IGoodsPriceListService goodsPriceListService) {
		this.goodsPriceListService = goodsPriceListService;
	}

    public IRegisterOrderService getRegisterOrderService() {
        return registerOrderService;
    }

    public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
        this.registerOrderService = registerOrderService;
    }

	public IUserLISTService getUserLISTService() {
		return userLISTService;
	}

	public void setUserLISTService(IUserLISTService userLISTService) {
		this.userLISTService = userLISTService;
	}
    
    
    

}
